Control Flow and Decision making Practice Problems in C
Posted on December 18, 2023 by Vishesh Namdev
Python
C
C++
Java
Basic Practice Problems based on chapter which you learn in previous Tutorials.
1. Even or Odd numbers
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// Check if the number is even or odd
if (number % 2 == 0) {
printf("%d is an even number.\n", number);
} else {
printf("%d is an odd number.\n", number);
}
return 0;
}
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// Check if the number is even or odd
if (number % 2 == 0) {
printf("%d is an even number.\n", number);
} else {
printf("%d is an odd number.\n", number);
}
return 0;
}
The program checks whether the entered number is even or odd using the modulus operator (%).
If the result of number % 2 is equal to 0, it means the number is even. Otherwise, it's odd.
The program then prints the result.
2. Vowel and Consonant
#include <stdio.h>
int main() {
char character;
printf("Enter a character: ");
scanf(" %c", &character); // Note the space before %c to consume the newline character
// Check if the character is an alphabet
if ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z')) {
// Check if the character is a vowel
if (character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u' ||
character == 'A' || character == 'E' || character == 'I' || character == 'O' || character == 'U') {
printf("%c is a vowel.\n", character);
} else {
printf("%c is a consonant.\n", character);
}
} else {
printf("Invalid input. Please enter an alphabet.\n");
}
return 0;
}
int main() {
char character;
printf("Enter a character: ");
scanf(" %c", &character); // Note the space before %c to consume the newline character
// Check if the character is an alphabet
if ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z')) {
// Check if the character is a vowel
if (character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u' ||
character == 'A' || character == 'E' || character == 'I' || character == 'O' || character == 'U') {
printf("%c is a vowel.\n", character);
} else {
printf("%c is a consonant.\n", character);
}
} else {
printf("Invalid input. Please enter an alphabet.\n");
}
return 0;
}
3. Roots of Quadratic Equations
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, discriminant, root1, root2;
printf("Enter coefficients (a, b, c) of the quadratic equation (ax^2 + bx + c = 0):\n");
scanf("%lf %lf %lf", &a, &b, &c);
// Calculate discriminant
discriminant = b * b - 4 * a * c;
// Check the nature of the roots
if (discriminant > 0) {
// Two real and distinct roots
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and distinct:\n");
printf("Root 1: %lf\n", root1);
printf("Root 2: %lf\n", root2);
} else if (discriminant == 0) {
// Two real and equal roots
root1 = -b / (2 * a);
printf("Roots are real and equal:\n");
printf("Root 1 = Root 2: %lf\n", root1);
} else {
// Complex roots
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and conjugate:\n");
printf("Root 1: %lf + %lfi\n", realPart, imaginaryPart);
printf("Root 2: %lf - %lfi\n", realPart, imaginaryPart);
}
return 0;
}
#include <math.h>
int main() {
double a, b, c, discriminant, root1, root2;
printf("Enter coefficients (a, b, c) of the quadratic equation (ax^2 + bx + c = 0):\n");
scanf("%lf %lf %lf", &a, &b, &c);
// Calculate discriminant
discriminant = b * b - 4 * a * c;
// Check the nature of the roots
if (discriminant > 0) {
// Two real and distinct roots
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and distinct:\n");
printf("Root 1: %lf\n", root1);
printf("Root 2: %lf\n", root2);
} else if (discriminant == 0) {
// Two real and equal roots
root1 = -b / (2 * a);
printf("Roots are real and equal:\n");
printf("Root 1 = Root 2: %lf\n", root1);
} else {
// Complex roots
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and conjugate:\n");
printf("Root 1: %lf + %lfi\n", realPart, imaginaryPart);
printf("Root 2: %lf - %lfi\n", realPart, imaginaryPart);
}
return 0;
}
4. Fibonacci Sequence
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
if (i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
return 0;
}
int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
if (i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
return 0;
}